﻿using Admin.Common;
using Admin.Model.Tool;
using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace Admin.Repository
{
    public class Repository : IDisposable
    {
        /// <summary>
        /// 数据库连接对象
        /// </summary>
        protected readonly IDbConnection _conn;

        /// <summary>
        /// 释放标记
        /// </summary>
        private bool disposed;

        public Repository()
        {
            _conn = new MySqlConnection(Appsettings.app(new string[] { "AppSettings", "MySql", "MySqlConnection" }));
            _conn.Open();
        }

        #region 增删改
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<T> BaseInsert<T>(string sql, object param, IDbTransaction transaction = null)
        {
            sql = $"{sql};select @@IDENTITY;";
            return await _conn.QueryFirstAsync<T>(sql, param, transaction);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> BaseEdit(string sql, object param, IDbTransaction transaction = null)
        {
            return await _conn.ExecuteAsync(sql, param, transaction);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> BaseDelete(string sql, object param, IDbTransaction transaction = null)
        {
            return await _conn.ExecuteAsync(sql, param, transaction);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <param name="IsCache">是否读取缓存</param>
        /// <param name="CacheTime">缓存时间，0表示不缓存</param>
        /// <returns></returns>
        public async Task<T> BaseGet<T>(string sql, object param, CacheModel cacheEntity = null)
        {
            return await _conn.QueryFirstOrDefaultAsync<T>(sql, param);
        }

        /// <summary>
        /// 通用列表查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <param name="cacheEntity"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T>> BaseGetList<T>(string sql, object param, CacheModel cacheEntity = null)
        {
            return await _conn.QueryAsync<T>(sql, param);
        }
        #endregion

        #region 其他
        public async Task<int> BaseQuery(string sql, object param)
        {
            return await _conn.QueryFirstAsync<int>(sql, param);
        }
        #endregion

        #region 垃圾回收
        /// <summary>
        /// 为了防止忘记显式的调用Dispose方法
        /// </summary>
        ~Repository()
        {
            //必须为false
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 非密封类可重写的Dispose方法，方便子类继承时可重写
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }
            //清理托管资源
            if (disposing)
            {
                //todo
            }
            if (_conn != null)
            {
                _conn.Dispose();
            }

            //告诉自己已经被释放
            disposed = true;
        }
        #endregion
    }
}
